package com.premedsidekick;


import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class New_Profile extends Activity {
	ProfileExpandableListAdapter adapter;
	String[] category_array;
	Integer[] categories = {
			R.id.progress_bar_label_1,
			R.id.progress_bar_label_2,
			R.id.progress_bar_label_3,
			R.id.progress_bar_label_4
	};
	Integer[] progresses = {
			R.id.progress_bar_status_1,
			R.id.progress_bar_status_2,
			R.id.progress_bar_status_3,
			R.id.progress_bar_status_4
	};
	
	String[] hours;
	
	Resources resources;
	TextView textview;
	ProgressBar bar;
	Cursor cursor;
	boolean quarter = false;
	int quarter_max = 12;
	int semester_max = 8;
	int clinical_max = 200;
	int community_max = 200;
	int research_max = 400;
	int physician_max = 35;
	int oral_max = 2;
	int poster_max = 2;
	
	int extracurricular_max = community_max + clinical_max + 
		research_max + physician_max + oral_max + poster_max;
	
	int overall_max;
	
	int extracurricular_progress, course_progress;
	String CLINICAL = "clinical_exposure_table";
	String COMMUNITY = "community_service_table";
	String RESEARCH = "research_table";
	String PHYSICIAN = "physician_table";
	String ORAL = "oral_table";
	String POSTER = "poster_table";
	
	//overall
	//course
	//extracurricular
	
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		
		setContentView(R.layout.new_profile_main);
		resources = getResources();
		hours = resources.getStringArray(R.array.hour_options);
		category_array = resources.getStringArray(R.array.profile_top);
		for (int i = 0; i < category_array.length; i++) {
			textview = (TextView)findViewById(categories[i]);
			textview.setText(category_array[i]);
		}

		bar = (ProgressBar)findViewById(progresses[2]);
		bar.setMax(extracurricular_max);
		extracurricular_progress = calculateExtracurricularProgress();
		bar.setProgress(extracurricular_progress);
		
		bar = (ProgressBar)findViewById(progresses[1]);
		course_progress = calculateCourseProgress();
		if (quarter == true) {
			bar.setMax(quarter_max);
			overall_max = quarter_max + extracurricular_max;
		}
		else {
			bar.setMax(semester_max);
			overall_max = semester_max + extracurricular_max;
		}
		
		bar.setProgress(course_progress);
		
		bar = (ProgressBar)findViewById(progresses[0]);
		bar.setMax(overall_max);
		bar.setProgress(course_progress + extracurricular_progress);
		
		ExpandableListView lv = (ExpandableListView)findViewById(R.id.new_profile_expandablelistview);
		lv.setDivider(null);
		adapter = new ProfileExpandableListAdapter(this);
		lv.setAdapter(adapter);
		//setListAdapter(adapter);
		//getListView().setDivider(null);
		//top_array = getResources().getStringArray(R.array.profile_top);

		 
	}

	public void setType(boolean _inc) {
		quarter = _inc;
	}
	public class ProfileExpandableListAdapter extends BaseExpandableListAdapter {
		Context context;
		Cursor cursor;
		LayoutInflater inflater;
		DefaultEntryDatabaseAdapter employmentDB;
		DefaultEntryDatabaseAdapter awardsDB;
		String[] headers = {"Employment", "Awards & Scholarships"};
		View view;
		TextView textview;
		String[] employment;
		String[] awards;
		ProfileExpandableListAdapter(Context context) {
			super();
			employmentDB = new DefaultEntryDatabaseAdapter(context, "employment_table");
			employmentDB.open();
			awardsDB = new DefaultEntryDatabaseAdapter(context, "awards_table");
			awardsDB.open();
			inflater = LayoutInflater.from(context);
			cursor = employmentDB.getAll();
			
			employment = new String[cursor.getCount()];
			cursor.moveToFirst();
			int i = 0;
			while (!cursor.isAfterLast()) {
				employment[i] = cursor.getString(1);
				cursor.moveToNext();
				i++;
			}
			i = 0;
			cursor.deactivate();
			cursor = awardsDB.getAll();
			
			awards = new String[cursor.getCount()];
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {
				awards[i] = cursor.getString(1);
				cursor.moveToNext();
				i++;
			}
			cursor.close();
			awardsDB.close();
			employmentDB.close();
			
		}
		@Override
		public Object getChild(int groupPosition, int childPosition) {
			String string = null;
			if (groupPosition == 0)
				string = employment[childPosition];
			else if (groupPosition == 1)
				string = awards[childPosition];
			return string;
		}
		@Override
		public long getChildId(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return childPosition;
		}
		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			convertView = inflater.inflate(R.layout.interview_footer, null);
			textview = (TextView)convertView.findViewById(R.id.interview_footer_textview_1);
			if (groupPosition == 0)
				textview.setText(employment[childPosition]);
			else
				textview.setText(awards[childPosition]);
			return convertView;
		}
		@Override
		public int getChildrenCount(int groupPosition) {
			if (groupPosition == 0)
				return employment.length;
			else
				return awards.length;
		}
		@Override
		public Object getGroup(int groupPosition) {
			if (groupPosition == 0)
				return employment[groupPosition];
			else
				return awards[groupPosition]; 
		}
		@Override
		public int getGroupCount() {
			// TODO Auto-generated method stub
			return 2;
		}
		@Override
		public long getGroupId(int groupPosition) {
			// TODO Auto-generated method stub
			return groupPosition;
		}
		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			convertView = inflater.inflate(R.layout.interview_header_not_expanded, null);
			textview = (TextView)convertView.findViewById(R.id.interview_header_textview_1);
			textview.setText(headers[groupPosition]);
			return convertView;
		}
		@Override
		public boolean hasStableIds() {
			// TODO Auto-generated method stub
			return true;
		}
		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return true;
		}
		
		
	}
	public int calculateCourseProgress() {
		int quarter_accumulator = 0;
		int semester_accumulator = 0;
		int returner = 0;
		
		DefaultCoursesDatabaseAdapter adapter = new DefaultCoursesDatabaseAdapter(New_Profile.this);
		//Temp_HorizontalScrollView.CourseRequirementsDatabaseAdapter adapter = view.new CourseRequirementsDatabaseAdapter(this);
		adapter.open();
		cursor.deactivate();
		cursor = adapter.getAllSemester();
		cursor.moveToFirst();
		while (!cursor.isAfterLast() && cursor != null) {
			if (!cursor.getString(0).equalsIgnoreCase("0")) {
				
				//System.out.println("Semester: " + cursor.getString(0));
				semester_accumulator++;
			}
			cursor.moveToNext();
		}
				
		cursor.deactivate();
		cursor = adapter.getAllQuarter();
		cursor.moveToFirst();
		while (!cursor.isAfterLast() && cursor != null) {
			if (!cursor.getString(0).equalsIgnoreCase("0")) {
				//System.out.println("T"+cursor.getString(0)+"T");
				quarter_accumulator++;
			}
			cursor.moveToNext();
		}
		float comparator_1 = quarter_accumulator / (float)quarter_max;
		float comparator_2 = semester_accumulator / (float)semester_max;
		if (comparator_1 > comparator_2) {
			//System.out.print("UP");
			setType(true);
			returner = quarter_accumulator;
		}
		else {
			//System.out.print("DOWN");
			setType(false);
			returner = semester_accumulator;
		}
		
		cursor.close();
		adapter.close();
		//System.out.println("Course progress: " + returner);
		return returner;
	}
	public int calculateExtracurricularProgress() {
		
		
		int accumulator = 0;
		int select;
		String string;
		DefaultDatabaseAdapter adapter = new DefaultDatabaseAdapter(this, CLINICAL);
		adapter.open();
		//cursor.deactivate();
		cursor = adapter.getHours();
		cursor.moveToFirst();
		while (!cursor.isAfterLast() && cursor != null) {
			string = cursor.getString(0);
			cursor.moveToNext();
			if (!string.equalsIgnoreCase("0")) {
				select = Integer.parseInt(string);
				string = hours[select];
				accumulator += Float.parseFloat(string);
			}
		}
		cursor.close();
		adapter.close();
		adapter = new DefaultDatabaseAdapter(this, COMMUNITY);
		adapter.open();
		cursor.deactivate();
		cursor = adapter.getHours();
		cursor.moveToFirst();
		while (!cursor.isAfterLast() && cursor != null) {
			string = cursor.getString(0);
			cursor.moveToNext();
			if (!string.equalsIgnoreCase("0")) {
				select = Integer.parseInt(string);
				string = hours[select];
				accumulator += Float.parseFloat(string);
			}
		}
		cursor.close();
		adapter.close();
		adapter = new DefaultDatabaseAdapter(this, RESEARCH);
		adapter.open();
		cursor.deactivate();
		cursor = adapter.getHours();
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			string = cursor.getString(0);
			cursor.moveToNext();
			if (!string.equalsIgnoreCase("0")) {
				select = Integer.parseInt(string);
				string = hours[select];
				accumulator += Float.parseFloat(string);
			}
		}
		cursor.close();
		adapter.close();
		adapter = new DefaultDatabaseAdapter(this, PHYSICIAN);
		adapter.open();
		cursor.deactivate();
		cursor = adapter.getHours();
		cursor.moveToFirst();
		while (!cursor.isAfterLast() && cursor != null) {
			string = cursor.getString(0);
			cursor.moveToNext();
			if (!string.equalsIgnoreCase("0")) {
				select = Integer.parseInt(string);
				string = hours[select];
				accumulator += Float.parseFloat(string);
			}
		}
		cursor.close();
		adapter.close();
		
		DefaultEntryDatabaseAdapter entry_adapter = new DefaultEntryDatabaseAdapter(this, ORAL);
		entry_adapter.open();
		cursor.deactivate();
		cursor = entry_adapter.getAll();
		cursor.moveToFirst();
		while (!cursor.isAfterLast() && cursor != null) {
			string = cursor.getString(0);
			cursor.moveToNext();
			if (!string.equalsIgnoreCase("Insert name"))
				accumulator++;
		}
		cursor.close();
		entry_adapter.close();
		
		entry_adapter = new DefaultEntryDatabaseAdapter(this, POSTER);
		entry_adapter.open();
		cursor.deactivate();
		cursor = entry_adapter.getAll();
		cursor.moveToFirst();
		while (!cursor.isAfterLast() && cursor != null) {
			string = cursor.getString(0);
			cursor.moveToNext();
			if (!string.equalsIgnoreCase("Insert name")) 
				accumulator++;
		}
		cursor.close();
		entry_adapter.close();
		
		//System.out.println("Extracurricular progress: " + accumulator);
		return accumulator;
	}
	public void onResume() {
		super.onResume();
		//setListAdapter(new ProfileListAdapter());
	}
	public void onDestroy() {
		super.onDestroy();		
	}
	
	public String CalculateTotalClinicalVolunteeringHours() {
		//System.out.println("Clinical Volunteering Accumulator gets called");
		float accumulator = 0;
		DefaultDatabaseAdapter dbHelper = new DefaultDatabaseAdapter(this, "clinical_exposure_table");
		dbHelper.open();
		
		Cursor whoop = dbHelper.getHours();
		//System.out.println("Count of whoop: " + whoop.getCount());
		//String[] hours = new String[cursor.getCount()];
		String[] selections = getResources().getStringArray(R.array.hour_options);
		whoop.moveToFirst();
		int i = 0;
		while (whoop.isAfterLast() == false) {
			accumulator += Float.parseFloat(selections[Integer.parseInt(whoop.getString(0))]);
			i++;
			whoop.moveToNext();
			//System.out.println(accumulator);
		}
		//for (String s: hours)
			//System.out.println("Hour value retrieved: " + selections[Integer.parseInt(s)]);
		whoop.close();
		dbHelper.close();
		String count = Float.toString(accumulator);
		return count;
	}
	public String CalculateTotalCommunityVolunteeringHours() {
		
		float accumulator = 0;
		DefaultDatabaseAdapter dbHelper = new DefaultDatabaseAdapter(this, "community_service_table");
		dbHelper.open();
		
		Cursor cursor = dbHelper.getHours();
		//String[] hours = new String[cursor.getCount()];
		String[] selections = getResources().getStringArray(R.array.hour_options);
		cursor.moveToFirst();
		int i = 0;
		while (cursor.isAfterLast() == false) {
			accumulator += Float.parseFloat(selections[Integer.parseInt(cursor.getString(0))]);
			i++;
			cursor.moveToNext();
		}
		//for (String s: hours)
			//System.out.println("Hour value retrieved: " + selections[Integer.parseInt(s)]);
		dbHelper.close();
		String count = Float.toString(accumulator);
		return count;
	}
	public String CalculateTotalResearchHours() {
		float accumulator = 0;
		DefaultDatabaseAdapter dbHelper = new DefaultDatabaseAdapter(this, "research_table");
		dbHelper.open();
		
		Cursor cursor = dbHelper.getHours();
		//String[] hours = new String[cursor.getCount()];
		String[] selections = getResources().getStringArray(R.array.hour_options);
		cursor.moveToFirst();
		int i = 0;
		while (cursor.isAfterLast() == false) {
			accumulator += Float.parseFloat(selections[Integer.parseInt(cursor.getString(0))]);
			i++;
			cursor.moveToNext();
		}
		//for (String s: hours)
			//System.out.println("Hour value retrieved: " + selections[Integer.parseInt(s)]);
		dbHelper.close();
		String count = Float.toString(accumulator);
		return count;
	}
	public static class Viewholder_TOP {
		ProgressBar progressbar;
		TextView textview;
	}
	public static class Viewholder_BOT {
		TextView textview_one;
		TextView textview_two;
	}
}

